<template>
  <div class="dialog">
    <el-dialog
      :title="info.isadd ? '添加菜单' : '编辑菜单'"
      :before-close="cancel"
      :visible.sync="info.isshow"
    >
      <el-form :model="menuForm" ref="menu"  :rules="rules">
        <el-form-item prop="title" label="菜单名称" :label-width="formLabelWidth">
          <el-input v-model="menuForm.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="上级菜单" :label-width="formLabelWidth">
          <el-select v-model="menuForm.pid" placeholder="请选择">
            <el-option label="顶级菜单" :value="0"></el-option>
            <el-option
              v-for="item in menulist"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="菜单类型" :label-width="formLabelWidth">
          <el-radio v-model="menuForm.type" :label="1">目录</el-radio>
          <el-radio v-model="menuForm.type" :label="2">菜单</el-radio>
        </el-form-item>
        <el-form-item v-if="menuForm.type==1" label="目录图表" :label-width="formLabelWidth">
          <el-input v-model="menuForm.icon" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item v-else label="菜单地址" :label-width="formLabelWidth">
          <el-input autocomplete="off" v-model="menuForm.url"></el-input>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            v-model="menuForm.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button @click="addInfo" v-if="info.isadd" type="primary"
          >添 加</el-button
        >
        <el-button @click="editInfo" v-else type="primary">编 辑</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {mapActions,mapGetters} from 'vuex'
import { menuADD, menuInfo, menuEdit } from "../../util/request/api";
export default {
  props: ["info"],
  data() {
    return {
      menuForm: {
        pid: 0,
        title: "",
        icon: "",
        type: 1,
        url: "",
        status: 1,
      },
       rules: {
        title: [
          { required: true, message: "请输入菜单名称", trigger: "blur" },
          { min: 2, max: 6, message: "长度在 2 到 6 个字符", trigger: "blur" },
        ],
      },
      formLabelWidth: "120px",
    };
  },
  computed: {
       ...mapGetters({
      menulist: "menu/getMenuList",
    }),
  },
  methods: {
    ...mapActions({
      getMenulist: "menu/getMenuAction",
    }),
    cancel() {
      this.$emit("cancel", false);
      this.empty();
    },
    //清空输入框
    empty() {
      this.menuForm = {
        pid: 0,
        title: "",
        icon: "",
        type: 1,
        url: "",
        status: 1,
      };
    },
    addInfo() {
      this.$refs.menu.validate((valid) => {
        if (valid) {
          menuADD(this.menuForm).then((res) => {
            console.log(res);
            if (res.code == 200) {
              this.$message.success(res.msg);
              this.cancel();
              this.getMenulist();
            } else {
              this.$message.error(res.msg);
               }  
          });
        }
      });
    },
    lookup(id) {
      console.log(id, "每一条数据的主key");
      menuInfo({ id }).then((res) => {
        if (res.code == 200) {
          console.log(res, "一条数据");
          this.menuForm = res.list;
          this.menuForm.id = id;
        }
      });
    },
    editInfo() {
      this.$refs.menu.validate((valid) => {
        if (valid) {
          menuEdit(this.menuForm).then((res) => {
            if (res.code == 200) {
              this.$message.success(res.msg);
              this.cancel();
              this.getMenulist();
            } else {
              this.$message.error(res.msg);
            }
          });
        } else {
        }
      });
    },
  },
};
</script>

<style scoped>
.dialog {
  position: absolute;
  top: 50%;
  left: 50%;
}
